/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IExtendModifyAssembler;
import com.inspur.edp.bff.api.manager.context.ModifyContext;
import com.inspur.edp.bff.spi.action.modify.AbstractModifyAction;
import com.inspur.edp.bff.spi.action.modify.AfterModifyAction;
import com.inspur.edp.bff.spi.action.modify.BeforeModifyAction;
import com.inspur.edp.bff.spi.action.modify.DefaultModifyAction;

import java.util.ArrayList;

public abstract class AbstractExtendModifyAssembler implements IExtendModifyAssembler {

    protected abstract ArrayList<BeforeModifyAction> getBeforeModifyActions(ModifyContext modifyContext);

    protected abstract ArrayList<AbstractModifyAction> getModifyActions(ModifyContext modifyContext);

    protected abstract ArrayList<AfterModifyAction> getAfterModifyActions(ModifyContext modifyContext);


    public final void doBeforeModify(IFSManagerContext context, ModifyContext modifyContext) {
        ArrayList<BeforeModifyAction> actions = getBeforeModifyActions(modifyContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (BeforeModifyAction action : actions) {
            action.deal(context);
        }
    }

    public final void doModify(IFSManagerContext context, ModifyContext modifyContext) {
        ArrayList<AbstractModifyAction> actions = getModifyActions(modifyContext);
        if (actions == null || actions.size() < 1) {
            actions = new ArrayList<AbstractModifyAction>();
            DefaultModifyAction action = new DefaultModifyAction(modifyContext);
            actions.add(action);
        }

        for (AbstractModifyAction action : actions) {
            action.deal(context);
        }
    }

    public final void doAfterModify(IFSManagerContext context, ModifyContext modifyContext) {
        ArrayList<AfterModifyAction> actions = getAfterModifyActions(modifyContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (AfterModifyAction action : actions) {
            action.deal(context);
        }
    }

}
